package com.yuan.yblog.search.pojo;

import com.yuan.yblog.crud.entity.Category;
import com.yuan.yblog.crud.entity.Post;
import com.yuan.yblog.crud.entity.User;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.Setting;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;

/**
 * @author hepeiyuan on 2024/5/26 21:08
 * @description
 */

@Data
@Document(indexName = "yblog-post")
@Setting(shards = 1,replicas = 0)
@Schema(description = "文章搜索结果实体")
public class EsPostDTO implements Serializable {
    @Serial
    private static final long serialVersionUID = -1L;

    @Id
    @Schema(description = "文章id")
    private Long id;

    @Field(type = FieldType.Keyword)
    @Schema(description = "文章标题")
    private String title;

    @Field(type = FieldType.Keyword)
    @Schema(description = "文章内容")
    private String content;

    @Schema(description = "评论数")
    private Integer commentCount;

    @Schema(description = "置顶等级")
    private Integer level;

    @Schema(description = "是否加精")
    private Integer recommend;

    @Schema(description = "创建日期")
    private Date created;

    @Schema(description = "作者名id")
    private Long userId;

    @Field(type = FieldType.Keyword)
    @Schema(description = "作者名")
    private String username;

    @Schema(description = "文章头像")
    private String avatar;

    @Schema(description = "分类id")
    private Long categoryId;

    @Schema(description = "分类名")
    private String categoryName;

    public static EsPostDTO getEsPostDTO(Post post, User user, Category category) {
        EsPostDTO esPostDTO = new EsPostDTO();
        if (post != null) {
            esPostDTO.setId(post.getId());
            esPostDTO.setTitle(post.getTitle());
            esPostDTO.setCommentCount(post.getCommentCount());
            esPostDTO.setLevel(post.getLevel());
            esPostDTO.setRecommend(post.getRecommend());
            esPostDTO.setCreated(post.getCreated());
        }
        if (user != null) {
            esPostDTO.setUserId(user.getId());
            esPostDTO.setUsername(user.getUsername());
            esPostDTO.setAvatar(user.getAvatar());
        }
        if (category != null) {
            esPostDTO.setCategoryId(category.getId());
            esPostDTO.setCategoryName(category.getName());
        }
        return  esPostDTO;
    }

}
